localStorage.removeItem("realName");
$.ajax({
    type: "POST",
    url: webConfig.webUrl + "/sseUser/logout",
    /*登出接口地址*/
    dataType: "json"
});



document.getElementById('userName').select();

function login() {
    var userName = document.getElementById('userName'),
        password = document.getElementById('password'),
        authCode = document.getElementById('authCode'),
        errorEle = $('.login_alert');

    if (!userName.value) {
        errorEle.html('用户名不能为空！');
        userName.select();
        return;
    }

    if (!authCode.value) {
        errorEle.html('验证码不能为空！');
        authCode.select();
        return;
    }else if(authCode.value.length != 6){
        errorEle.html('请输入6位验证码！');
        authCode.select();
        return;
    }
    if(!password.value){
        errorEle.html('密码不能为空！');
        password.select();
        return;
    }else if (password.value.length < 6 || password.value.length > 20){
        password.select();
        errorEle.html('请输入6-20位密码！');
        return;
    }
    $.ajax({
        url: webConfig.webUrl + '/sseUser/login',
        type: 'POST',
        dataType: 'json',
        data: {
            userName: userName.value,
            password: password.value,
            authCode: authCode.value
        },
        success: function(data) {
            if (data) {
                if (data.code == webConfig.code.SUCCESS) {
                    localStorage.setItem("realName", data.result.userInfoVo.realName);
                    window.location.href = "index.html";
                } else {
                    parent.layer.alert(data.message, {
                        icon: 2
                    });
                }
            }
        }
    });
}

/**
 * 获取邮箱验证码
 * @param  {[type]} val [按钮元素]
 * @return {[type]}     [description]
 */
function getVerCode(val){
    var username = document.getElementById('userName'),
        password = document.getElementById('password'),
        errorEle = $('.login_alert');
    var countdown=60;
    if(!username.value){
        errorEle.html('用户名不能为空！');
        username.select();
        return;
    }
    errorEle.html('');
    val.setAttribute("disabled", true);
    $.ajax({
        type:   "POST",
        url :   webConfig.webUrl+"/sseAuthCode/sendAuthCode",//验证码接口地址
        dataType:"json",
        data:{
            username:username.value
        },
        success: function(data){
            if (data.code == webConfig.code.SUCCESS){
                var timer = setInterval(function() {
                    if (countdown === 0) {
                        val.removeAttribute("disabled");
                        val.value="获取验证码";
                        countdown = 60;
                        clearInterval(timer);
                    } else {

                        val.value= countdown + "s";
                        countdown--;
                    }
                },1000);
                layer.msg(data.message);
                return;
            }else{
                layer.alert(data.message,{icon:2});
            }
        }
    });
}


bodyKeyDown(login);